# This is a sample Python script. # Press Shift+F10 to execute it or replace it with your code. # Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. 


import sys

import qrcode


def file_to_qrcode(input_file, output_file):
    try:
        with open(input_file, "rb") as f:
            file_data = f.read()
    except FileNotFoundError as e:
        print(e)
        sys.exit(1)
    if len(file_data) > 2950:
        print("error file too big,max size 3kb")
        sys.exit(1)
    print("load file:", input_file)
    try:
        content = file_data.decode("utf-8")
    except UnicodeDecodeError as e:
        print(e)
        sys.exit(1)
    qr = qrcode.QRCode(version=None, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4)

    qr.add_data(content)

    qr.make(fit=True)

    img = qr.make_image(fill_color="black", back_color="white")
    img.save(output_file)

    print("saved qrcode:", output_file)
    pass


if __name__ == '__main__':

    if len(sys.argv) != 3:
        print("usage: input.txt output.jpg")
        sys.exit(1)
    input_file = sys.argv[1]
    output_file = sys.argv[2]
    file_to_qrcode(input_file, output_file)
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
